// service/withdrawal/withdrawal.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agree: false,
    txWay:0,
    info: '',
    money: '',
    type: '',
    num: '',
    token: ''
  },
  clickread() {
    this.setData({
      agree: !this.data.agree
    })
  },
  infoTap(e){
    let info = e.currentTarget.dataset.info
    let name = e.currentTarget.dataset.name
    let id = app.globalData.config[info]
    util.skip('/service/info/info?id='+id+'&name='+name)
  },
  // 跳转支付宝账户
  tobindAccount(e){
    wx.navigateTo({
      url: '/service/bindAccount/bindAccount?account=' + e.currentTarget.dataset.account,
    })
  },
  bankWay(){
    this.setData({
      txWay:1
    })
  },
  all(){
    this.setData({
      num: this.data.money
    })
  },
  AlipayWay(){
    this.setData({
      txWay:0
    })
  },
  withdraw(){
    if (!this.data.agree) {
      util.toast('请先阅读并同意《提现协议》')
      return
    }
    if(Number(this.data.num) <= 0) return util.toast('请输入正确的金额')
    if(Number(this.data.num) > Number(this.data.money)) return util.toast('提现金额不能大于'+this.data.money)
    if(this.data.txWay == 0 && (!this.data.info.alipay_name || !this.data.info.alipay_account))  return util.toast('请绑定支付宝账户')
    if(this.data.txWay == 1 && (!this.data.info.bank_user || !this.data.info.bank_name || !this.data.info.bank_card))  return util.toast('请绑定银行卡账户信息')
    http.post('withdraw/apply',{
      withdrawtype: this.data.txWay,
      type: this.data.type,
      num: this.data.num,
      types: 'withdraw'
    }).then(res=> {
      util.toast(res.msg)
      this.getInfo()
    })
  },
  setNum(e){
    this.setData({
      num: e.detail.value
    })
  },
  getInfo(){
    http.get('index/updateuser').then(res=> {
      this.setData({
        info: res.data,
      })
      if(this.data.type == 1){
        this.setData({
          info: res.data,
          money: res.data.shop_money
        })
      }else if(this.data.type == 2){
        this.setData({
          info: res.data,
          money: res.data.shop_user_money
        })
      }else if(this.data.type == 3){
        this.setData({
          info: res.data,
          money: res.data.skillEnsurePrice
        })
      }else if(this.data.type == 4){
        this.setData({
          info: res.data,
          money: res.data.shopEnsurePrice
        })
      }else {
        this.setData({
          info: res.data,
          money: res.data.money
        })
      }
      
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type: options.type,
      token: wx.getStorageSync('token')
    })
   
  },

  

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getInfo()
  },

  
})